//
//  ParallaxController.m
//  CustomDemo
//
//  Created by 余意 on 2016/12/20.
//  Copyright © 2016年 余意. All rights reserved.
//

#import "ParallaxViewController.h"

#import "PSCell.h"

@interface ParallaxViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView * PSTableView;

@end

@implementation ParallaxViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.PSTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)PSTableView
{
    if (!_PSTableView)
    {
        _PSTableView = [UITableView new];
        _PSTableView.delegate = self;
        _PSTableView.dataSource = self;
        _PSTableView.rowHeight = PARALLAXCELL_HEIGHT;
        _PSTableView.backgroundColor = RANDOMCOLOR ;
        [_PSTableView registerClass:[PSCell class] forCellReuseIdentifier:@"Cell"];
        [self.view addSubview:_PSTableView];
        _PSTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _PSTableView.frame = self.view.bounds;
    }
    return _PSTableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PSCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = RANDOMCOLOR ;
    cell.backIV.image = [UIImage imageNamed:[NSString stringWithFormat:@"parallax_%ld.jpg",indexPath.row % 4 + 1]];
    //    cell.titleLab.text = @"Misaki";
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    //1、cell 出现时的效果 防止加载时滑动卡顿
    CATransform3D rotation;//3D旋转
    
    rotation = CATransform3DMakeTranslation(0 ,50 ,20);
    // 逆时针旋转
    
    rotation = CATransform3DScale(rotation, 0.9, .9, 1);
    
    rotation.m34 = 1.0/ -600;
    
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    
    cell.layer.transform = rotation;
    
    [UIView beginAnimations:@"rotation" context:NULL];
    //旋转时间
    [UIView setAnimationDuration:0.6];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
     */
    
    // 2、滚动视差效果实现
    PSCell * displayCell = (PSCell *)cell;
    [displayCell celloffset];
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    PSCell * displayCell = (PSCell *)cell;
    [displayCell cancelAnimation];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSArray <PSCell *> *array = [self.PSTableView visibleCells];
    
    [array enumerateObjectsUsingBlock:^(PSCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj celloffset];
    }];
}

@end
